﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using EXT;
using System.Windows.Forms;
using DevExpress.Utils;

namespace BanHangCshap
{
    public partial class frmLoadMa : Form
    {
        public frmLoadMa()
        {
            InitializeComponent();
        }

        Class.KhachHangNCC cls = new BanHangCshap.Class.KhachHangNCC();
        Class.PhieuNhapXuat cls1 = new BanHangCshap.Class.PhieuNhapXuat();
        List<DataTable> Ls = new List<DataTable>();
        List<string> Lss = new List<string>();
        List<string> Lsss = new List<string>();
        List<string> Lssss = new List<string>();

        private void simpleButton1_Click(object sender, EventArgs e)
        {
            WaitDialogForm dg = new WaitDialogForm("Đang load dữ liệu...", "Xin chờ trong giây lát.");
            DataTable dt = cls.layDSKhachHangNCCTheoLoai(1);
            int xCount = dt.Rows.Count;
            for (int i = 0; i < xCount; i++)
            {
                string str ="select p.manhapxuat, p.ngaynhapxuat, p.loainhapxuat, p.id from phieunhapxuat p where makhncc ='"+dt.Rows[i]["makhncc"].ToString()+"' order by ngaynhapxuat desc, manhapxuat desc";
                DataTable x = cls.getDataTable(str);
                int y =x.Rows.Count;
                for (int j = 0; j < y; j++)
                {
                    if (y > 1)
                    {
                        if (j == y-1)
                        {
                            break;
                        }
                        if (x.Rows[j]["id"].ToInt32() == x.Rows[j + 1]["id"].ToInt32())
                        {
                            Ls.Add(x);
                            Lss.Add(str);
                            Lsss.Add(dt.Rows[i]["makhncc"].ToString());
                            Lssss.Add(dt.Rows[i]["tenkhncc"].ToString());
                            break;
                        }
                    }
                }
            }
            groupControl1.Text = "Có " + Ls.Count.ToString() + " khách hàng bị lỗi";
            dg.Close();
        }

        int x = 0;
        private void simpleButton2_Click(object sender, EventArgs e)
        {
            WaitDialogForm dg = new WaitDialogForm("Đang xử lý dữ liệu...", "Xin chờ trong giây lát.");
     
            if (x < Ls.Count)
            {
                DataTable ddt = cls.getDataTable("select p.mathuchi, p.ngaythuchi, p.loaithuchi, p.id from phieuthuchi p where makhncc ='" + Lsss[x] + "' order by ngaythuchi desc, mathuchi desc");
                gridControl2.DataSource = ddt;
                gridControl1.DataSource = Ls[x];
                int xx = Ls[x].Rows.Count;

                for (int i = 0; i < xx-1; i++)
                {
                    if (Ls[x].Rows[i]["id"].ToString() == Ls[x].Rows[i+1]["id"].ToString())
                    {
                        DataRow r1 = gridView1.GetDataRow(i);
                        DataRow r2 = gridView1.GetDataRow(i+1);
                        r1.SetColumnError(1, "bác này trùng id");
                        r2.SetColumnError(1, "bác này trùng id");
                    }
                    int k =ddt.Rows.Count;
                    for (int j = 0; j <k; j++)
                    {
                        if (Ls[x].Rows[i]["id"].ToString()==ddt.Rows[j]["id"].ToString())
                        {
                            DataRow r1 = gridView3.GetDataRow(j);
                            //DataRow r2 = gridView1
                            r1.SetColumnError(0, "bác này trùng id");
                        }
                    }
                }
                memoEdit2.Text = Lss[x];
                
                groupControl1.Text = "Khách hàng thứ: " + x.ToString() + "__" + Lsss[x] + "__"+Lssss[x];
            }
            x++;
            dg.Close();
        }

        private void simpleButton3_Click(object sender, EventArgs e)
        {
            x = 0;
        }

        private void gridView1_DoubleClick(object sender, EventArgs e)
        {
            memoEdit2.Text = "update Phieunhapxuat set id='" + (gridView1.GetFocusedRowCellValue("id").ToInt32() + 1).ToInt32() + "', sort ='" + gridView1.GetFocusedRowCellValue("manhapxuat").ToString().Substring(2, 8) + (gridView1.GetFocusedRowCellValue("id").ToInt32() + 1).ToInt32().ToString("00000#") + "' where manhapxuat ='" + gridView1.GetFocusedRowCellValue("manhapxuat").ToString() + "'";
            this.Text = "Nam Mô A Di Đà Phật_" + cls.executeQuery(memoEdit2.Text).ToString();
        }

        private void frmLoadMa_Load(object sender, EventArgs e)
        {
            gridView1.GroupPanelText = "Danh sách các phiếu nhập xuất của khách";
            gridView2.GroupPanelText = "Danh sách sác phiếu thu chi";
        }

        private void memoEdit2_Click(object sender, EventArgs e)
        {
            memoEdit2.SelectAll();
            try
            {
                Clipboard.SetText(memoEdit2.EditValue.ToString());
            }
            catch (Exception)
            {
                MessageBox.Show("Chả có gì để coppy");
            }
        }
    }
}
